<template>
  <CompOperate
      :field="field"
      :designer="designer"
      :design-state="designState"
      :parent-list="parentList"
      :parent-widget="parentWidget"
      :sub-form-row-id="subFormRowId"
      :sub-form-row-index="subFormRowIndex"
      :sub-form-col-index="subFormColIndex"
      :index-of-parent-list="indexOfParentList"
      :display-style="field.options.displayStyle"
  >
    <div class="scene-window-patient-widget" :style="getStyle('area')">
      <div class="window-area flex-box">
        <div class="flex-box-left flex-item">
          <div class="left-wrapper" :style="getStyle('win')">
            <div class="location" :style="getStyle('room')">
              <ScrollX>{{ getRoomInfo }}</ScrollX>
            </div>
            <div class="number" :style="getStyle('number')">
              {{
                optionsInfo['roomNumberAuto'] ?
                    getWindowDeptInfo[optionsInfo['roomNoKey'] || 'roomNo'] : optionsInfo['roomNumber']
              }}
            </div>
          </div>
        </div>

        <div class="flex-box-right flex-item">
          <div class="current-area right-item">
            <div class="title" :style="getStyle('currentTitle')">{{(optionsInfo.currentPatientTitle)||'正在采血'}}</div>
            <template v-for="(item,key) in getRoomDoctorInfo('treatingPatient')">
              <div class="content" v-if="key===0">
                <p :style="getStyle('currentContent')">
                  {{ item.orderNo || '\u00a0' }}<br
                    v-if="optionsInfo['currentPatientConnectWrap']"/>{{ item.patientName || '\u00a0' }}
                </p>
              </div>
            </template>
          </div>
        </div>
      </div>
    </div>
  </CompOperate>
</template>

<script>
import {NoticeBar, Swipe, SwipeItem} from "vant";
import emitter from "@/utils/designer/emitter";
import {pxToVw} from '@/utils/common';
import ScrollY from '@c/public/seamlessScroll/ScrollY.vue';
import ScrollX from '@c/public/seamlessScroll/ScrollX.vue';
import CompOperate from "@c/public/form-designer/devType/modules/compOperate.vue";
import mixin from "@sc/templateDetails/modules/homeMiddle/fieldMixin";
import {buildParentApi} from "@/utils/designer/util";
import vueSeamlessScroll from 'vue-seamless-scroll';
import {setWindowDeptInfo} from "@/store/modules/actions";

export default {
  name: "scene-window-current-patient-1920-widget",

  componentName: "FieldWidget", //必须固定为FieldWidget，用于接收父级组件的broadcast事件

  mixins: [emitter, mixin],

  components: {
    ScrollX,
    CompOperate,
    NoticeBar,
    Swipe,
    SwipeItem,
    ScrollY,
    vueSeamlessScroll
  },

  props: {
    widget: Object,
    devType: {
      type: Boolean,
      default: false,
    },
    field: Object,
    parentWidget: Object,
    parentList: Array,
    indexOfParentList: Number,
    designer: Object,
    designState: {
      type: Boolean,
      default: false,
    },
    subFormRowIndex: {
      /* 子表单组件行索引，从0开始计数 */ type: Number,
      default: -1,
    },
    subFormColIndex: {
      /* 子表单组件列索引，从0开始计数 */ type: Number,
      default: -1,
    },
    subFormRowId: {
      /* 子表单组件行Id，唯一id且不可变 */ type: String,
      default: "",
    },
    headTitle: {
      type: String,
      default: "",
    },
    visibleTitle: {
      type: Boolean,
    },
    //prop传入的表单JSON配置
    parentApi: {
      type: Object,
      default: () => buildParentApi()
    },
  },

  data() {
    return {
      optionsInfo: Object(),
      header: require("@a/images/design/header.jpeg"),
      pause: require("@a/images/design/pause.png"),
    }
  },

  computed: {
    // 模板配置行内样式
    getStyle() {
      return function (val) {
        const self = this;
        let info = (self.optionsInfo) || Array();
        let Obj = Object();
        console.log(info)
        switch (val) {
          case 'area':
            Reflect.set(Obj, 'height', pxToVw(info.areaHeight));
            return Obj;
          case 'win':
            Reflect.set(Obj, 'background', (info.roomBackground));
            return Obj;
          case 'room':
            Reflect.set(Obj, 'fontSize', pxToVw(info.roomNameFontSize));
            Reflect.set(Obj, 'background', (info.roomNameBackground));
            Reflect.set(Obj, 'color', (info.roomNameColor));
            return Obj;
          case 'number':
            Reflect.set(Obj, 'fontSize', pxToVw(info.roomNumberFontSize));
            Reflect.set(Obj, 'color', info.roomNumberColor);
            return Obj;
          case 'currentTitle':
            Reflect.set(Obj, 'fontSize', pxToVw(info.currentPatientTitleFontSize));
            Reflect.set(Obj, 'color', (info.currentPatientTitleColor));
            Reflect.set(Obj, 'background', (info.currentPatientConnectBackground));
            return Obj;
          case 'currentContent':
            Reflect.set(Obj, 'fontSize', pxToVw(info.currentPatientConnectFontSize));
            Reflect.set(Obj, 'color', (info.currentPatientConnectColor));
            return Obj;
        }
      }
    },

    // 获取科室信息
    getWindowDeptInfo() {
      let devType = (this.devType);
      let roomDoctorInfo = this.$store.getters['getWindowDeptInfoInfo'];
      if (devType) {
        return roomDoctorInfo.apiData;
      } else {
        return roomDoctorInfo.mockData;
      }
    },

    //获取医生-患者信息
    getRoomDoctorInfo() {
      return function (type) {
        let temp = Array();
        let devType = (this.devType);
        let roomDoctorInfo = this.$store.getters['getRoomDoctorInfo'];
        let data;
        if (devType) {
          data = roomDoctorInfo.apiData || Array()
        } else {
          data = roomDoctorInfo.mockData || Array()
        }

        if (data?.length) {
          // 截取第一位医生
          let topPlace = data.slice(0, 1);
          // 获取首位医生的就诊和候诊的患者--数组
          return topPlace[0][type] || Array();
        } else {
          temp.push({})
          return temp[0][type] || Array();
        }
      }
    },

    getRoomInfo() {
      const self = this;
      let info = (self.optionsInfo) || Array();
      let deptInfo = (this.getWindowDeptInfo);
      if (info['roomNameAuto']) {
        if (!!info && !!info['roomNameKey']) {
          return deptInfo[info['roomNameKey']] || deptInfo['roomName']
        } else {
          return deptInfo['roomName']
        }
      } else {
        return info['roomName']
      }
    }
  },

  created() {
    this.registerToRefList();
    this.initEventHandler();
    this.handleOnCreated();
  },

  mounted() {
    this.handleOnMounted();
  },

  methods: {
    // 接收父组件传递过来的数据
    executeMethod(key, data) {
      switch (key) {
        case "roomDoctorInfo":
          console.log(`医生-患者-信息-${data}`);
          this.$store.dispatch('setRoomDoctorInfo', this.$lodash.cloneDeep(data))
          break
        case "roomDeptInfo":
          console.log(`科室诊室信息-${data}`)
          this.$store.dispatch('setWindowDeptInfo', this.$lodash.cloneDeep(data))
          break
      }
    },
  },


  watch: {
    field: {
      handler: function (val) {
        let optionsInfo = (val.options[val.options.config]);
        if (!Reflect.has(optionsInfo, 'deptName')) {
          Reflect.set(optionsInfo, 'deptName', '采血处')
        }
        this.optionsInfo = optionsInfo;
      },
      deep: true,
      immediate: true
    }
  },

  destroyed() {
    this.unregisterFromRefList();
  },

};
</script>

<style lang="scss" scoped>
.scene-window-patient-widget {
  color: #333;
  height: 150px;
  font-size: 40px;
  padding: 28px 30px;
  box-sizing: border-box;
  flex: inherit;
  flex-direction: column;
  justify-content: space-between;
  background: #E5EAEF;;
  overflow: hidden;

  .window-area {
    height: 100%;
    background: transparent;

    .flex-item {
      overflow: inherit;
      border: none;
    }

    .flex-box-left {
      margin-right: 14px;
      color: white;
      overflow: hidden;
      border-radius: 20px;
      text-align: center;
      flex-shrink: 0;
      min-width: calc(44% - 14px) !important;
      flex-direction: column;
      box-shadow: 30px 30px 30px -10px rgba(0, 0, 0, 0.15),
      inset 15px 15px 10px rgba(255, 255, 255, 0.75),
      -15px -15px 35px rgba(255, 255, 255, 0.55),
      inset -1px -1px 10px rgba(0, 0, 0, 0.2);
      .left-wrapper{
        height: 100%;
        .location {
          min-width: 100%;
          flex-basis: 100%;
          height: 298px;
          min-height: 298px;
          max-height: 298px;
          display: flex;
          font-size: 150px;
          align-items: center;
          justify-content: center;
        }

        .number {
          width: 100%;
          height: calc(100% - 298px);
          font-size: 500px;
          font-weight: bolder;
          align-items: center;
          align-self: center;
          justify-content: center;
        }
      }

    }

    .flex-box-right {
      margin-left: 14px;
      flex-shrink: 0;
      min-width: calc(56% - 14px) !important;
      flex-direction: column;
      background: transparent;

      .right-item {
        width: 100%;
        height: 100%;
        overflow: hidden;
        border-radius: 20px;
        background: white;
        display: flex;
        flex-direction: column;
        border: 1px rgba(0, 0, 0, 0.2) solid;
        box-shadow: 30px 30px 30px -10px rgba(0, 0, 0, 0.15),
        inset 15px 15px 10px rgba(255, 255, 255, 0.75),
        -15px -15px 35px rgba(255, 255, 255, 0.55),
        inset -1px -1px 10px rgba(0, 0, 0, 0.2);

        .title {
          width: 100%;
          height: 298px;
          font-size: 150px;
          color: #0059B3;
          display: flex;
          justify-content: center;
          text-align: center;
          align-items: center;
          align-self: center;
          @include fontWeight();
          background: rgba(221, 221, 221, 0.4);
        }

        .content {
          width: 100%;
          height: calc(100% - 298px);
          display: flex;
          align-self: center;
          justify-content: center;
          align-items: center;
          overflow: hidden;
          p {
            font-size: 140px;
            width: 100%;
            @include fontWeight();
            text-align: center;
            color: #333333;
            line-height: 1.4;

            white-space: nowrap;
            @include singleEllipsis(100%)
          }
        }
      }
    }

  }
}
</style>
